package cn.iocoder.yudao.module.member.service.siteconfiguration;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.module.member.controller.admin.siteconfig.vo.SiteConfigurationVo;
import cn.iocoder.yudao.module.member.controller.admin.siteconfig.vo.UserPoolConfigDetailVo;
import cn.iocoder.yudao.module.member.controller.admin.siteconfig.vo.UserPoolConfigVo;
import cn.iocoder.yudao.module.member.convert.siteconfiguration.UserPoolConfigConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.siteconfiguration.UserPoolConfigDo;
import cn.iocoder.yudao.module.member.dal.mysql.siteconfiguration.UserPoolConfigMapper;
import cn.iocoder.yudao.module.member.dal.redis.no.OrderNoRedisDAO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Validated
public class UserPoolConfigServiceImpl implements UserPoolConfigService{


    @Resource
    private UserPoolConfigMapper userPoolConfigMapper;
    @Resource
    private UserPoolConfigDetailService userPoolConfigDetailService;
    @Resource
    private OrderNoRedisDAO orderNoRedisDAO;


    private static final String SITE_CONFIG_USER = "SCU";
    private static final String VERSION_TYPE_CODE = "VT";

    /**
     * 默认前六种数据不可更改编码
     */

    private static final String PREFIX_CODE = "version:type:code";


    /**
     * 保存前端站点配置信息
     * @param saveReqVO
     */
    @Override
    public void saveConfig(SiteConfigurationVo saveReqVO) {

        if (CollectionUtil.isEmpty(saveReqVO.getUserPoolConfigs())){
            return;
        }
        saveReqVO.getUserPoolConfigs().forEach(t->{
            String generate = orderNoRedisDAO.generateNoPrefix(SITE_CONFIG_USER);
            t.setSiteUserCode(generate);
            t.setId(null);
            if (StrUtil.isEmpty(t.getVersionTypeCode())){
                String versionTypeCode =  orderNoRedisDAO.generate(VERSION_TYPE_CODE);
                t.setVersionTypeCode(versionTypeCode);
            }
        });
        List<UserPoolConfigDo> userPoolConfigDos = UserPoolConfigConvert.INSTANCE.convertList((saveReqVO.getUserPoolConfigs()));
        userPoolConfigMapper.insertBatch(userPoolConfigDos);
        userPoolConfigDetailService.saveConfig(saveReqVO.getUserPoolConfigs());
    }

    @Override
    public List<UserPoolConfigVo> getConfig() {
        List<UserPoolConfigDo> list = userPoolConfigMapper.selectList();
        List<UserPoolConfigVo> userPoolConfigVos = UserPoolConfigConvert.INSTANCE.convertList2(list);
        List<UserPoolConfigDetailVo> allConfig = userPoolConfigDetailService.getAllConfig();
        if (CollUtil.isEmpty(allConfig)){
            return userPoolConfigVos;
        }
        Map<String, List<UserPoolConfigDetailVo>> collect = allConfig.stream().collect(Collectors.groupingBy(UserPoolConfigDetailVo::getSiteUserCode));
        userPoolConfigVos.forEach(t->{
            t.setDetails(collect.get(t.getSiteUserCode()));
        });
        return userPoolConfigVos;
    }

    /**
     * 更具主表id删除数据
     *
     * @param code
     */
    @Override
    public void deleteBySiteCode(String code) {
        userPoolConfigMapper.deleteBySiteCode(code);
        userPoolConfigDetailService.deleteBySiteCode(code);
    }

    /**
     * 单独修改配置信息
     *
     * @param vo
     * @return
     */
    @Override
    public void updateConfig(UserPoolConfigVo vo) {
        UserPoolConfigDo userPoolConfigDo =   userPoolConfigMapper.getConfigByUserCode(vo.getSiteUserCode());
        if (ObjectUtil.isNull(userPoolConfigDo)){
            throw new RuntimeException("未找到配置信息");
        }
        userPoolConfigDetailService.deleteBySiteUserCode(userPoolConfigDo.getSiteUserCode());
        userPoolConfigDo.setVersionTypeName(vo.getVersionTypeName());
        userPoolConfigMapper.updateById(userPoolConfigDo);
        vo.setSiteUserCode(userPoolConfigDo.getSiteUserCode());
        vo.setSiteCode(userPoolConfigDo.getSiteCode());
        List<UserPoolConfigVo> vos = new ArrayList<>();
        vos.add(vo);
        userPoolConfigDetailService.saveConfig(vos);

    }
}
